<template>
  <div>
    <van-nav-bar
      class="top"
      title="注册账号"
      left-arrow
      @click-left="$router.back()"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>
    <router-link class="link" to="/login">有账号，去登录</router-link>
  </div>
</template>

<script>
import { register } from '@/api/user'

export default {
  name: 'login-page',
  components: {},
  props: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  methods: {
    async onSubmit (val) {
      // console.log('submit', val)
      const res = await register(val)
      console.log(res)
      if (res.status === 200) {
        this.$toast.success(res.description)
        this.$router.push('/login')
      } else {
        this.$toast.fail(res.description)
      }
    }
  }
}
</script>

<style scoped lang="less">
.top {
  background-color: #56b37b;
  /deep/ .van-nav-bar__title {
    color: #fff;
  }
  /deep/.van-icon-arrow-left {
    color: #fff;
  }
}
.link {
  color: gray;
  font-size: 12px;
  margin-left: 35%;
}
</style>
